package com.proposition.web.paper;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.proposition.commons.constant.ErrorCode;
import com.proposition.commons.constant.PaperPermission;
import com.proposition.commons.model.ExamCategory;
import com.proposition.commons.model.Paper;
import com.proposition.paper.service.PaperService;
import com.proposition.support.annotation.LogAnnotation;
import com.proposition.support.utils.Rest;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *  试卷管理控制器
 * @author 杨凯
 * @create 2018-01-31 14:15
 **/
@Slf4j
@Controller
@RequestMapping("/paper")
public class PaperController {

	@Autowired
	private PaperService paperService;

	/**
	 * 试卷列表界面
	 * @return
	 */
	@RequiresPermissions(value={PaperPermission.CHECK_ONE_PAPER,PaperPermission.CHECK_TWO_PAPER,PaperPermission.CHECK_THREE_PAPER,PaperPermission.DELETE_PAPER,PaperPermission.UPDATE_PAPER},logical= Logical.OR)
	@GetMapping("/index")
	public String index(Model model) {
		model.addAttribute("yearList", paperService.selectYearList());
		return "paper/paper-list";
	}


	/**
	 *
	 * @param start 开始页码
	 * @param length 每页显示长度
	 * @param paperPattern 试卷站类型
	 * @param checkStatus 试卷审核状态
	 * @param type  A/B卷
	 * @param name  试卷审核名称
	 * @param year  年度
	 * @return
	 */
	@LogAnnotation(moduleName = "试卷模块", operate = "获取试卷列表")
	@ResponseBody
	@RequestMapping(value = "/list", method = RequestMethod.GET, produces = "application/json")
	public Map paperList(@RequestParam(defaultValue = "1") int start,
	                     @RequestParam(defaultValue = "10") int length,
	                     @RequestParam(required = false) String paperPattern,
	                     @RequestParam(required = false) String checkStatus,
	                     @RequestParam(required = false) String type,
	                     @RequestParam(required = false) String name,
	                     @RequestParam(required = false) String year) {
		Map<String, Object> map = new HashMap<>(3);
		EntityWrapper<Paper> wrapper = new EntityWrapper<>();
		wrapper.like("name", name);
		if (!StringUtils.isEmpty(type)) {
			wrapper.eq("type", type);
		}
		if (!StringUtils.isEmpty(checkStatus)) {
			wrapper.eq("check_status", checkStatus);
		}
		if (!StringUtils.isEmpty(paperPattern)) {
			wrapper.eq("paper_pattern", paperPattern);
		}
		if (!StringUtils.isEmpty(year)) {
			wrapper.eq("year", year);
		}
		Page page = new Page(start / length + 1, length);
		Page<Paper> paperPage = paperService.selectPage(page, wrapper);
		List<Paper> list = paperPage.getRecords();
		map.put("data", list);
		map.put("iTotalRecords", paperPage.getTotal());
		map.put("iTotalDisplayRecords", paperPage.getTotal());
		return map;
	}

	/**
	 * 删除试卷信息
	 * @param paperId 试卷Id
	 * @return
	 */
	@LogAnnotation(moduleName = "试卷模块", operate = "试卷删除")
	@ResponseBody
	@PostMapping(value = "/delete")
	public Rest deletePaperById(@RequestParam("paperId") String paperId) {
		Rest.Builder rest = Rest.newBuilder();
		try {
			Subject subject = SecurityUtils.getSubject();
			if (!subject.isPermitted(PaperPermission.DELETE_PAPER)) {
				return rest.status(ErrorCode.PERMISSION_DENIED).build();
			}
			paperService.deletePaper(paperId);
		} catch (Exception e) {
			log.error("试卷删除失败[{}]", e.getMessage());
			return rest.status(ErrorCode.UNEXPECTED_ERROR).build();
		}
		return rest.status(ErrorCode.SUCCESS).build();
	}

	/**
	 * 获取试卷详情
	 * @param model
	 * @param paperId 试卷Id
	 * @return
	 */
	@LogAnnotation(moduleName = "试卷模块", operate = "试卷详情")
	@GetMapping("/detail/{paperId}")
	public String paperDetail(Model model, @PathVariable String paperId) {
		//获取试卷详细信息
		Paper paper = paperService.selectById(paperId);
		Map<String, Object> map = paperService.getPaperDetail(paperId);
		model.addAttribute("paper", paper);
		model.addAttribute("paperExam", map);
		return "paper/paper-detail";
	}

	/**
	 * 试卷修改界面
	 * @param model
	 * @param paperId
	 * @return
	 */
	@GetMapping("/update/{paperId}")
	public String update(Model model, @PathVariable("paperId") String paperId) {
		model.addAttribute("examClass", paperService.findExamClass());
		model.addAttribute("paper", paperService.selectById(paperId));
		return "paper/paper-update";
	}

	/**
	 * 获取试题分类子分类
	 * @param id
	 * @return
	 */
	@GetMapping("/exam/category")
	@ResponseBody
	public List<ExamCategory> getListByExamClassId(Integer id) {
		return paperService.findExamCategory(id);
	}
}
